﻿using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using XYAC.Framework.MonitorTireMaterialOnline.Context;
using XYAC.Framework.MonitorTireMaterialOnline.Entities;
using XYAC.Framework.MonitorTireMaterialOnline.Extensions;
using XYAC.Framework.MonitorTireMaterialOnline.IServices;

namespace XYAC.Framework.MonitorTireMaterialOnline.Services
{
    public class LocalReportService : ServiceBase<int, CurrentReportContext>, ILocalReportService
    {
        public LocalReportService(IOptionsMonitor<AppConfig> appConfig, ILogger<LocalReportService> logger, IServiceProvider provider) : base(appConfig, logger, provider)
        {

        }

        public int DecoupToReport(DateTime? date = null)
        {
            const int rowCount = 20;
            int ret = 0;
            try
            {
                using (var context = new CustomReportContext(this.AppConfig, DateOnly.FromDateTime(date ?? DateTime.Now.Date)))
                {
                    using (var trans = context.Database.BeginTransaction())
                    {
                        try
                        {
                            var data = context.Set<SpecValLocal>().Where(T => T.DecoupState != true).OrderBy(T => T.ID).Take(rowCount).ToList();
                            if (data.Count == 0)
                            {
                                return 0;
                            }
                            else
                            {
                                var insertList = data.Select(T =>
                                {
                                    if (T.ProfileType == 1)//胎侧
                                    {
                                        ReportEntityBase ent = T.To<SemiSteelSideInspectionReport>();
                                        
                                        ent.SpecValId = T.ID.Value;
                                        return ent;
                                    }
                                    else//胎面
                                    {
                                        ReportEntityBase ent = T.To<SemiSteelTreadScanReport>();
                                        ent.SpecValId = T.ID.Value;
                                        return ent;
                                    }
                                }).ToList();


                                foreach (var entity in data)
                                {
                                    entity.DecoupState = true;
                                }
                                context.Set<SpecValLocal>().UpdateRange(data);
                                var sideInspectionReports = insertList.Where(T => T is SemiSteelSideInspectionReport).Select(T => T as SemiSteelSideInspectionReport).ToList();
                                if (sideInspectionReports.Count != 0)
                                    context.Set<SemiSteelSideInspectionReport>().AddRange(sideInspectionReports);
                                var steelTreadScanReports = insertList.Where(T => T is SemiSteelTreadScanReport).Select(T => T as SemiSteelTreadScanReport).ToList();
                                if (steelTreadScanReports.Count != 0)
                                    context.Set<SemiSteelTreadScanReport>().AddRange(steelTreadScanReports);

                                int tempRet = context.SaveChanges();
                                if (tempRet == data.Count * 2)
                                {
                                    trans.Commit();
                                    ret = tempRet / 2;
                                }
                                else
                                {
                                    trans.Rollback();
                                }


                            }
                        }
                        catch (Exception ex)
                        {
                            trans.Rollback();
                            Logger?.LogError($"解耦数据失败:{ex.Message}", ex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger?.LogError($"解耦数据失败:{ex.Message}", ex);
            }

            return ret;

        }
    }
}
